<template>
  <div class="login_container">
    <vue-particles
          color="#fff"
          :particleOpacity="0.9"
          :particlesNumber="240"
          shapeType="circle"
          :particleSize="1.5"
          linesColor="#FFFFFF"
          :linesWidth="2"
          :lineLinked="false"
          :lineOpacity="0.4"
          :linesDistance="150"
          :moveSpeed="2"
          :hoverEffect="false"
          hoverMode="grab"
          :clickEffect="false"
          clickMode="push"
          class="cash"
        >
        </vue-particles>
    <div class="login_box">
      <!--头像区域-->
      <div class="avatar_box">
        <img src="../assets/pic/logo.png" alt="">
      </div>
      <div class="projectName">
        三三WMS--管理系统
      </div>
      <!--登录表单区-->
      <el-form ref="loginFormRef" class="login_form" :model="loginForm" :rules="loginFormRules" label-width="0px">
        <!--用户名-->
        <el-form-item prop="userName" v-on:keyup.enter.native="login">
          <el-input prefix-icon="iconfont icon-user" v-focus v-model="loginForm.userName"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="userPassword" v-on:keyup.enter.native="login">
          <el-input prefix-icon="iconfont icon-mima" v-model="loginForm.userPassword" type="password"></el-input>
        </el-form-item>
        <!--按钮-->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>

export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        userName: '',
        userPassword: ''
      },
      loginFormRules: {
        userName: [{
          required: true,
          message: '请输入用户名称',
          trigger: 'blur'
        },
        {
          min: 3,
          max: 10,
          message: '长度在 6 到 10 个字符',
          trigger: 'blur'
        }
        ],
        userPassword: [{
          required: true,
          message: '请输入用户密码',
          trigger: 'blur'
        },
        {
          min: 6,
          max: 10,
          message: '长度在 6 到 10 个字符',
          trigger: 'blur'
        }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const res = await this.$apiUtil.postByForm('user/login', this.loginForm)
        console.log(res)
        if (res.code !== '200') {
          return this.$message.error(res.msg)
        }
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', res.data.token)
        window.sessionStorage.removeItem('roleName')
        window.sessionStorage.setItem('roleName', res.data.roleName)
        if (window.sessionStorage.getItem('activePath')) {
          await this.$router.push(window.sessionStorage.getItem('activePath'))
        } else {
          await this.$router.push('/main')
        }
      })
    }
  },
  directives: {
    focus: {
      inserted: function (el) {
        el.querySelector('input').focus()
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .login_container {
    background-color: #2b4b6b;
    background: url(../assets/pic/bg.jpg);
    height: 100%;
    background-size: cover;
  }
  .projectName{
        color: #ffffff;
        text-align: center;
        padding-top: 97px;
        font-size: 26px;
  }
  .login_box {
    width: 450px;
    height: 340px;
    background-color: #1c5996;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box {
      height: 130px;
      width: 130px;
      // border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      // box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #66b1ff;

      img {
        width: 110px;
        height: 110px;
        margin-top: 10px;
        margin-left: 10px;
        border-radius: 50%;
        background-color: #66b1ff;
      }
    }
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }
  .cash {
      position:fixed;
      top:0px;
      width:100%;
      height: 1000px;
      overflow: hidden;
    }
</style>
